#!/usr/bin/perl

use strict;
use lib '.';

use RDF::Redland;
use Swignition::DataModel::Model;
use Swignition::DataModel::Graph;
use Swignition::DataModel::Triple;
use Swignition::DataModel::BlankNode;
use Swignition::DataModel::Resource;
use Swignition::DataModel::Literal;
use Swignition::DataModel::XMLLiteral;
use Swignition::DataModel::Collection;

my $model  = Swignition::DataModel::Model->new;
my $graph1 = Swignition::DataModel::Graph->new($model)->add_uri('http://example.com/graph1');
my $graph2 = Swignition::DataModel::Graph->new($model)->add_uri('http://example.com/graph2');

$model->combined_graph->add_uri('http://example.com/combined.graph');

my $triple = $graph1->quick_rrr('_:Bob',
                                'http://xmlns.com/foaf/0.1/knows',
                                '_:Dave');
$graph1->quick_rrr('_:Bob',
	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
	'http://xmlns.com/foaf/0.1/Person');
print $triple->subject->a('http://xmlns.com/foaf/0.1/Person', [$graph2]) ? "Bob is a Person\n" : "Bob is not a Person\n";

# The _:Bob here is a different Bob.
$graph2->quick_rrr('_:Bob',
                   'http://xmlns.com/foaf/0.1/knows',
                   '_:Kevin');
$graph2->quick_rrl('_:Bob',
                   'http://xmlns.com/foaf/0.1/name',
                   'Bob',
                   undef,
                   'en');

# But we can add a triple to graph2 which includes the original Bob!
$graph2->quick_rrr('_:Bob',
                   'http://xmlns.com/foaf/0.1/knows',
                    $triple->subject);

my $list = Swignition::DataModel::Collection->new($model, 'List');
$graph2->quick_rrr('_:Bob',
                   'http://example.com/some_list',
                   $list);

$list->add_node(Swignition::DataModel::Resource->new($model, 'http://example.com/one'));
$list->add_node(Swignition::DataModel::Resource->new($model, 'http://example.com/two'));
$list->add_node(Swignition::DataModel::Resource->new($model, 'http://example.com/three'));
$list->add_node(Swignition::DataModel::Resource->new($model, 'http://example.com/four'));

use Data::Dumper;

my @r = $graph2->sparql('SELECT ?x ?y WHERE { ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?y .}');
foreach my $r (@r)
{
	print $r->{'x'}->identity . " rdf:type <" . $r->{'y'}->identity . "> .\n";
}

print "---\n";

my $rl = $model->combined_graph->as_redland;
my $serializer = RDF::Redland::Serializer->new("ntriples");
print $serializer->serialize_model_to_string(RDF::Redland::URI->new('http://dbpedia.org/resource/'), $rl);

print "---\n";

print Dumper($model->combined_graph->as_json('gt')) . "\n";

__END__

use Data::Dumper;
my $i = 0;

my $model  = Swignition::DataModel::Model->new;
my $graph  = Swignition::DataModel::Graph->new($model);
my $graph2 = Swignition::DataModel::Graph->new($model);

$graph->quick_rrl('http://tobyinkster.co.uk/~tobyink/#', 
	'http://xmlns.com/foaf/0.1/name',
	'Toby Inkster',
	undef,
	'en_GB');
$graph->quick_rrr('http://dbpedia.org/resource/Toby_Inkster', 
	'http://xmlns.com/foaf/0.1/page',
	'http://tobyinkster.co.uk');
$graph->quick_rrl('_:n1', 'http://xmlns.com/foaf/0.1/name', 'Freddy', undef, 'en');
$graph->quick_rrl('_:n1', 'http://xmlns.com/foaf/0.1/name', 'Frederick', undef, 'de');

$graph->lookup_node('http://dbpedia.org/resource/Toby_Inkster')->merge_with(
	$graph->lookup_node('http://tobyinkster.co.uk/~tobyink/#'));

my $triple = $graph2->quick_rrl('_:n1', 'http://xmlns.com/foaf/0.1/name', 'Fred', undef, 'en');
$graph2->quick_rrr($triple, 'http://xmlns.com/foaf/0.1/maker', '_:Fred');

$triple->reify(1);

my $rl = $model->combined_graph->as_redland;
my $serializer = RDF::Redland::Serializer->new("ntriples");
print $serializer->serialize_model_to_string(RDF::Redland::URI->new('http://dbpedia.org/resource/'), $rl);
print "--------\n";

$triple->unreify;

my $rl = $model->combined_graph->as_redland;
my $serializer = RDF::Redland::Serializer->new("ntriples");
print $serializer->serialize_model_to_string(RDF::Redland::URI->new('http://dbpedia.org/resource/'), $rl);
print "--------\n";

# print Dumper([$graph->lookup_node('http://tobyinkster.co.uk/~tobyink/#')->uris]);


__END__

my $me       = Swignition::DataModel::Resource->new($model,
		'http://tobyinkster.co.uk/~tobyink/#');
my $me2      = Swignition::DataModel::Resource->new($model,
		'http://dbpedia.org/resource/Toby_Inkster');
my $foafname = Swignition::DataModel::Resource->new($model,
		'http://xmlns.com/foaf/0.1/name');
my $toby     = Swignition::DataModel::Literal->new($model,
		'Toby Inkster', 'en');
my $foafpage = Swignition::DataModel::Resource->new($model,
		'http://xmlns.com/foaf/0.1/page');
my $ticu     = Swignition::DataModel::Resource->new($model,
		'http://tobyinkster.co.uk');


my $graph = $model->combined_graph;
$graph->quick($me,  $foafname, $toby);
$graph->quick($me2, $foafpage, $ticu);
$me->merge_with($me2);

print Dumper($graph->as_json);
print Dumper($model);

#printf("%s\n", $foafname->uri);
#printf("%s\n", $toby->string);
#print scalar(@{$foafname->subject_of});

